package com.toman.ui.menu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.mikephil.charting.formatter.IFillFormatter;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.toman.Interfaces.NewBaseUrl;
import com.toman.JavaBean.BeltlineInspectEntity;
import com.toman.JavaBean.DeviceEntity;
import com.toman.JavaBean.SysMenuEntityNew;
import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.IntentUtils;
import com.toman.Util.JsonExcludeNull;
import com.toman.Util.SPUtils;
import com.toman.Util.StringUtils;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.toolbar.ToolBarActivity;
import com.toman.ui.menu.adapter.IndexDataAdapter;
import com.toman.ui.work.device.DeviceRepairActivity;
import com.toman.ui.work.device.DeviceRepairAddActivity;
import com.toman.widget.MyGridView;
import com.ywanhzy.demo.AppConfig;
import com.ywanhzy.demo.AppContext;
import com.ywanhzy.demo.entity.MenuEntity;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Request;
import okhttp3.Response;

public class MenuFirstActivity extends ToolBarActivity {
    private static AppContext appContext;
    private IndexDataAdapter adapter;
    private List<MenuEntity> indexDataList = new ArrayList<MenuEntity>();
    @BindView(R.id.gv_lanuch_start)
    MyGridView gridView;
    @BindView(R.id.sv_index)
    ScrollView svIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu_first);
        ButterKnife.bind(this);
        initToolBar("应用首页");
        initView();
    }

    private void initView() {
        appContext = (AppContext) getApplication();
        gridView.setFocusable(true);
        adapter = new IndexDataAdapter(this, indexDataList);
gridView.setAdapter(adapter);
getSelect();
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();

                String title = indexDataList.get(position).getTitle();
                String strId = indexDataList.get(position).getId();
                if (strId.equals("all")) {// 更多
                    intent.setClass(MenuFirstActivity.this, com.toman.ui.menu.MenuManageActivity.class);
                    startActivity(intent);
                }
                else
                {MenuJump.go(MenuFirstActivity.this,title);}


            }
        });
    }


    public void getSelect() {
        OkHttpUtils
                .get(NewBaseUrl.company_customusermenu_list)
                .params("token", token)
                .params("userId", (String) SPUtils.get(getApplicationContext(),"userid",""))
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String result, Request request, @Nullable Response response) {
                        Log.i("test", "选择的项目: " + result);

                        JSONObject jsonObj = JSON.parseObject(result);
                        int code = jsonObj.getInteger("code");ViewUtil.lagout(code,getApplicationContext());
                        if (code == 0) {
                            List<SysMenuEntityNew> menuList = new ArrayList<SysMenuEntityNew>(JsonExcludeNull.parseArrayExcludeNull(jsonObj.getString("menuList"), SysMenuEntityNew.class));
                            indexDataList.clear();
                            for (SysMenuEntityNew entityNew : menuList) {
                                MenuEntity menuEntity=new MenuEntity();
                                menuEntity.setTitle(entityNew.getName());
                                menuEntity.setIco(entityNew.getAppIconUrl());
                                menuEntity.setId(entityNew.getMenuId());
                                if (StringUtils.isNotEmpty(entityNew.getIsUse())){
                                    if (entityNew.getIsUse().equals("1")){
                                        menuEntity.setSelect(true);
                                    }
                                }

                                indexDataList.add(menuEntity);
                            }
                        appContext.saveObject((Serializable) indexDataList, AppConfig.KEY_USER);
                            MenuEntity allMenuEntity=new MenuEntity();
                            allMenuEntity.setId("all");
                            allMenuEntity.setTitle("全部");
                            indexDataList.add(allMenuEntity);
                            adapter.notifyDataSetChanged();


                        }

                        else {
                            ToastUtil.makeText(getApplicationContext(), jsonObj.getString("msg"));
                            finish();
                        }

                    }
                });
    }
    @Override
    protected void onResume() {
        super.onResume();
        indexDataList.clear();
        indexDataList = (List<MenuEntity>) appContext.readObject(AppConfig.KEY_USER);
        MenuEntity allMenuEntity=new MenuEntity();
        allMenuEntity.setId("all");
        allMenuEntity.setTitle("全部");
        indexDataList.add(allMenuEntity);
        adapter = new IndexDataAdapter(MenuFirstActivity.this, indexDataList);
        gridView.setAdapter(adapter);
    }
}
